﻿using System.Data;
using System.Collections.Generic;
using System.ComponentModel;
using System;
using CommonLib;
using Constructor.Frames;

namespace Constructor
{
    class SharedFuncs
    {
        public static string GetFrameStringInfo(Frame frame)
        {
            string result = string.Empty;
            result = frame.Name + "\nТип фрейма " + frame.Type.ToString();
            if (frame.ContainsAKOSlot())
                result += "\n(" + frame.Name + " наследуется от " + frame.GetSlots(SlotType.AKO, false)[0].Subframe.Name + ")";
            foreach (Slot slot in frame.AllSlots)
                result += "\n------------------------\n" + GetSlotStringInfo(slot);
            return result;
        }

        public static string GetSlotStringInfo(Slot slot)
        {
            string result = string.Empty;
            result = slot.Name + "\nТип слота " + slot.Type.ToString();
            if (slot.Type == SlotType.SubFrame || slot.Type == SlotType.Next || slot.Type == SlotType.Error || slot.Type == SlotType.AKO)
            {
                result += "\n" + slot.Type.ToString() + ": " + slot.Subframe.Name + "(" + slot.Subframe.Type.ToString() + ")";
            }
            if (slot.Type == SlotType.Production)
            {
                result += "\nПроцедурный: " + slot.Production.isProc + " Маркер: " + slot.Production.Marker +
                    "\n" + slot.Production.Item.Text + slot.Production.Operation;
                if (slot.Production.isProc)
                {
                    result += slot.Production.Procedure.Method + "(";
                    foreach (NewItem param in slot.Production.Procedure.Params)
                        result += param.Text + ", ";
                    result += ")";
                }
                else
                {
                    result += slot.Production.Value.Text;
                }
            }
            if (slot.Type == SlotType.Regular)
            {
                if (slot.Frame.Type != FrameType.Exemplar)
                    result += "\nТип наследования: " + slot.Inherit.ToString();
                result += "\nЗадания отсутствия: " + slot.Default.ToString();
            }
            return result;
        }

        public static bool DataSetEquals(DataSet ds1, DataSet ds2)
        {
            if (ds1 == null && ds2 == null)
                return true;
            if (ds1 == null && ds2 != null || ds2 == null && ds1 != null)
                return false;
            if (ds1.Tables.Count != ds2.Tables.Count)
                return false;
            foreach (DataTable dt1 in ds1.Tables)
            {
                if (!ds2.Tables.Contains(dt1.TableName)
                    || dt1.Columns.Count != ds2.Tables[dt1.TableName].Columns.Count
                    || dt1.Rows.Count != ds2.Tables[dt1.TableName].Rows.Count)
                    return false;
                DataTable dt2 = ds2.Tables[dt1.TableName];
                foreach (DataColumn dc in dt1.Columns)
                    if (!dt2.Columns.Contains(dc.ColumnName))
                        return false;
                for (int j = 0; j < dt1.Rows.Count; j++)
                {
                    for (int i = 0; i < dt1.Rows[j].ItemArray.Length; i++)
                        if (dt1.Rows[j].ItemArray[i].ToString() != dt2.Rows[j].ItemArray[i].ToString())
                            return false;
                }
            }
            return true;
        }

        public static DataSet CloneDataset(DataSet ds)
        {
            DataSet result = new DataSet(ds.DataSetName);
            foreach (DataTable dt in ds.Tables)
            {
                DataTable added = result.Tables.Add(dt.TableName);
                foreach (DataColumn dc in dt.Columns)
                    added.Columns.Add(dc.ColumnName);
                foreach (DataRow dr in dt.Rows)
                    added.Rows.Add(dr.ItemArray);
            }
            return result;
        }

        public static string[] LastOpened
        { get { return Properties.Settings.Default.lastOpened.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); } }

        public static void AddLastOpened(string value)
        {
            List<string> lo = new List<string>(Properties.Settings.Default.lastOpened.Split('|'));
            if (lo.Contains(value))
                lo.RemoveAll(o => o == value);
            for (int i = 0; i < Math.Min(6, lo.Count); i++)
                value += "|" + lo[i];
            Properties.Settings.Default.lastOpened = value;
            Properties.Settings.Default.Save();
        }

        public static void RemoveLastOpened(string value)
        {
            List<string> lo = new List<string>(Properties.Settings.Default.lastOpened.Split('|'));
            if (lo.Contains(value))
            {
                lo.Remove(value);
                if (lo.Count > 0)
                {
                    string str = lo[0];
                    for (int i = 0; i < lo.Count; i++)
                        str += "|" + lo[i];
                    Properties.Settings.Default.lastOpened = str;
                    Properties.Settings.Default.Save();
                }
            }
        }
    }
}
